package com.followmeandroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.followmeandroid.GCM.GCMRegister;
import com.followmeandroid.models.CurrentMember;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;

/**
 * <p>Title: MainActivity</p>
 * <p>Description: This activity is the main activity of the application.
 * It controls the access to other activities within the application.</p>
 * @team Adam Drews
 * @email adam.drews@my.uwrf.edu
 * @team Tyler Johnson
 * @email tyler.johnson@my.uwrf.edu
 * @team Nicholas Thorp
 * @email nicholas.thorp@my.uwrf.edu
 * @date 11/17/2014
 */
public class MainActivity extends Activity {
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    static final String TAG = "MainActivity - GCM";
    Context context;
    String regid;


    Button buttonViewMap, buttonViewFriends, buttonViewSettings, buttonViewCurrentLocation;
    private View.OnClickListener buttonNewMapListener = new View.OnClickListener() {
        /**
         * Launches the Maps activity.
         * @param v     The current view.
         */
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, MapsActivity.class);
            startActivity(intent);
        }
    };
    private View.OnClickListener buttonNewFriendListener = new View.OnClickListener() {
        /**
         * Launches the Friend List activity.
         * @param v     The current view.
         */
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, FriendListActivity.class);
            startActivity(intent);
        }
    };
    private View.OnClickListener buttonNewSettingListener = new View.OnClickListener() {
        /**
         * Launches the Settings Activity.
         * @param v     The current view.
         */
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(intent);
        }
    };
    private View.OnClickListener buttonNewCurrentLocationListener = new View.OnClickListener() {
        /**
         * Launches the Location activity.
         * @param v     The current view.
         */
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, LocationActivity.class);
            startActivity(intent);
        }
    };

    /**
     * Called when the activity is first created.
     * @param savedInstanceState    The state of the previous activity.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeComponents();

        context = getApplicationContext();

        // Check device for Play Services APK. If check succeeds, proceed with
        //  GCM registration.
        if (checkPlayServices() && CurrentMember.getMember() != null) {
            GoogleCloudMessaging.getInstance(this);
            regid = GCMRegister.getRegistrationId(context);

            if (regid.isEmpty()) {
                GCMRegister.registerInBackground(context);
            }else if(CurrentMember.getMember().getRegistrationID() == null || CurrentMember.getMember().getRegistrationID().toString().length() < 10){
                GCMRegister.sendToServer(this, regid);
            }
        } else {
            Log.i(TAG, "No valid Google Play Services APK found.");
        }
        //GcmIntentService intasdf = new GcmIntentService();
    }

    /**
     * Creates the visual compenents, like the buttons to launch the other
     * activities.
     */
    private void initializeComponents() {
        buttonViewMap = (Button) findViewById(R.id.buttonNewMap);
        buttonViewFriends = (Button) findViewById(R.id.buttonNewViewFriends);
        buttonViewSettings = (Button) findViewById(R.id.buttonNewViewSettings);
        buttonViewCurrentLocation = (Button) findViewById(R.id.buttonNewViewCurrentLocation);

        buttonViewMap.setOnClickListener(buttonNewMapListener);
        buttonViewFriends.setOnClickListener(buttonNewFriendListener);
        buttonViewSettings.setOnClickListener(buttonNewSettingListener);
        buttonViewCurrentLocation.setOnClickListener((buttonNewCurrentLocationListener));
    }

    /**
     * Checks to see if Google Play Services are available.
     * @return  true    Available.
     *          false   Unavailable.
     */
    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i(TAG, "This device is not supported.");
                finish();
            }
            return false;
        }
        return true;
    }
}

